package com.zhang.bingmei_incomeexpenditure.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.bingmei_incomeexpenditure.entity.commom.DataScope;
import com.zhang.bingmei_incomeexpenditure.entity.user.User;
import com.zhang.bingmei_incomeexpenditure.mapper.UserMapper;
import com.zhang.bingmei_incomeexpenditure.service.UserService;
import org.springframework.stereotype.Service;

/**
 * 系统用户Service实现类
 * @author auto generate
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 获取数据权限
     * @param userId 当前用户ID
     * @param level 用户等级：1系统管理员 2省级管理员 3市级管理员 4区县级管理员
     * @return
     */
    @Override
    public DataScope getDataScope(String userId, Integer level) {
        User user = getById(userId);
        int minLevel = level;

        String area = user.getArea();
        DataScope dataScope = new DataScope();
        dataScope.setUserId(user.getUserId());
        dataScope.setMinLevel(minLevel);
        if (StringUtils.isNotBlank(area)){
            String[] areaList = area.split(",");
            //1系统管理员和2省级管理员管辖区域都为海南省
            if (minLevel == 1 || minLevel == 2) {
                dataScope.setArea("460000");
                //3 市县级管理员
            } else if (minLevel == 3) {
                if (areaList.length >= 2) {
                    dataScope.setArea(areaList[0]+","+areaList[1]);
                }
                //4区级管理员
            } else if (minLevel == 4) {
                if ((area.contains("460100") || area.contains("460200") || area.contains("460300")) && areaList.length >= 3) {
                    dataScope.setArea(areaList[0]+","+areaList[1]+","+areaList[2]);
                }
            }
        }
        return dataScope;
    }
}